<template>
	<view class="cart-login">
		<image src="../../static/logo.png"></image>
		<view class="title">优选商城</view>
		<view class="btn">
			<up-button type="success" @click="showPop">微信一键授权登录</up-button>
		</view>
	</view>
	<view class="pop">
		<up-popup :show="show" @close="close" @open="open" bgColor='#434343' round='10'>
			<view class="pop-content">
				<view class="pop-head">
					<image src="../../static/logo.png"></image>
					<view class="">优选</view>
				</view>
				<view class="text1">申请微信授权登录</view>
				<view class="text2">用于注册、登录小程序</view>
				<view class="btn">
					<button class="btn-refuse" @click="refuse">拒绝</button>
					<button class="btn-agree" @click="agree">允许</button>
				</view>
			</view>
		</up-popup>
	</view>

</template>

<script setup>
	import {
		post,
		baseUrl
	} from '/hooks/req.js';
	import {
		useUserStore,
		useCart,
		useUserAddressStore,
	} from '@/store/index';
	import {
		onHide,
		onLoad
	} from "@dcloudio/uni-app"
	import {
		ref
	} from 'vue';
	//用户信息
	let useUserInfo = useUserStore()
	let userAddressInfo = useUserAddressStore()
	const show = ref(false);
	const userCart = useCart()
	const showPop = () => {
		show.value = true
	}
	const open = () => {
		show.value = true
	}
	const close = () => {
		show.value = false
	}
	const agree = () => {
		login()
		uni.showLoading({
			title: "加载中",
			mask: true,
		})
		setTimeout(() => {
			uni.hideLoading();
		}, 2000)
		//从商品详情页登录完成后回到详情页
		// 获取当前页面栈数组
		const pages = getCurrentPages();
		//判断是否有多页，否则获取上一个页面的路径时会报错
		if (pages.length >1) {
			// 获取上一个页面
			const prevPage = pages[pages.length - 2];
			// 获取上一个页面的路径
			const prevPagePath = prevPage.route;
			// 判断上一个页面是否为特定页面
			if (prevPagePath === 'pages/goodsDetail/goodsDetail') {
				// 是特定页面
				setTimeout(() => {
					uni.navigateBack()
				}, 1000)

			} else {
				// 不是特定页面
				console.log('上一个页面不是特定页面');
			}
		}

	}

	const refuse = () => {
		console.log("取消授权");
		close()
	}
	const login = () => {
		//获取用户信息
		uni.getUserProfile({
			desc: '获取用户信息',
			lang: "zh_CN",
			success: (res) => {
				useUserInfo.userInfo.avatarUrl = res.userInfo.avatarUrl
				useUserInfo.userInfo.nickName = res.userInfo.nickName
				//调用微信登录接口获取临时code
				uni.login({
					provider: "weixin",
					success: (res) => {
						if (res.code) {
							// 组装后端接口需要的数据
							const params = ref([{
								code: res.code,
								avatarUrl: useUserInfo.userInfo.avatarUrl,
								userName: useUserInfo.userInfo.nickName,
							}]);
							// 调用登录接口获取token
							post("/uniAppLogin", params.value).then(result => {
								const data = {
									id: result.data.data.uniId,
									nickName: result.data.data.nickName,
									avatarUrl: result.data.data.avatarUrl,
									token: result.data.data.token,
								}
								//存储用户信息
								useUserInfo.userInfo = data
								//将token存储到本地
								uni.setStorage({
									key: "token",
									data: result.data.data.token
								})
								uni.setStorage({
									key: "user",
									data: useUserInfo.userInfo
								})
								userAddressInfo.loadAddressList();
								userCart.loadShoppingcartList();
							})

						}

					}
				})
			}
		})
		
	}
	onHide(() => {
		show.value = false;
	})
</script>

<style lang="scss" scoped>
	.cart-login {
		height: 100vh;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;

		image {
			width: 80px;
			height: 80px;
		}

		.title {
			font-size: 16px;
		}

		.btn {
			margin-top: 20px;
		}
	}

	.pop-content {
		color: white;
		padding: 20px;

		.pop-head {
			display: flex;
			font-size: 16px;
			color: white;
			align-items: center;
			margin-bottom: 10px;

			image {
				width: 30px;
				height: 30px;
				margin-right: 10px;
			}
		}

		.text1 {
			font-weight: bold;
			font-size: 18px;
		}

		.text2 {
			color: #a4a4a4;
			font-size: 12px;
			margin-bottom: 10px;
		}

		.btn {
			margin-top: 30px;
			display: flex;

			button {
				width: 100px;
			}

			.btn-agree {
				background-color: #3bd221;
				color: white;
			}
		}
	}
</style>